var uploader = {
    'data':{
        'token':'',
        'uploadDomain':'',
        'domain':''
    },
    'complete':function(res){
        var url="http://"+uploader.data.domain +"/"+ res.key;
        $(".pic-pic").attr("src",url);
        $(".hidden-pic").val(res.key);
    },
    'error':function () {
        console.dir(arguments);
    },
    // 分片上传时的进度展示功能在此书写.
    'next':function (response) {
        // var chunks = response.chunks || [];
        // var total = response.total;
    },
    'init':function(){
        $.ajax({
            'url':'/upload/get-token',
            'type':'POST',
            'dataType':'json',
            'success':function(data){
                if(data.code != 200)
                {
                    layer.alert(data.msg);
                    return false;
                }
                uploader.data.token = data.data.token;      // token
                uploader.data.domain = data.data.domain;    // 显示域名
                // uploader.data.uploadDomain = 'idv093d.qiniudns.com';    // 上传域名
                console.log(uploader.data.domain)
            }
        });
    },
    'eventBind':function(){
        this.init();
        $('.default').on('change',function () {
            var token = uploader.data.token;
            var config = {
                useCdnDomain: true,
                disableStatisticsReport: false,
                region: qiniu.region.z0
            };
            var putExtra = {
                fname: "",
                params: {},
                mimeType: ["image/png", "image/jpeg", "image/gif"], // 限制文件类型.
            };
            var file = this.files[0];
            var observable;
            if (file) {
                var files=$(".default")[0].files[0];    // 用来显示的文件名.需要修改,重名时覆盖
                var key1=file.name;
                var key=key1.split(".")[0]+randomNum()+"."+key1.split(".")[1];
                console.log(key)
                putExtra.params["x:name"] = key.split(".")[0];
                // 配置完成的数组.
                var subObject = {
                    next: uploader.next,
                    error: uploader.error,
                    complete: uploader.complete
                };
                var subscription;
                // 调用sdk上传接口获得相应的observable，控制上传和暂停
                observable = qiniu.upload(files, key, token, putExtra, config);
                subscription = observable.subscribe(subObject);
                // subscription.unsubscribe();  // 取消上传.
            }
        });
    }
};
$(document).ready(function(){
    uploader.eventBind();

    var icon="";
    var flag=true;
    var token="";
    $(".openchoose").on("click",function(){
        $(".filter").css("display","block");
    });
    $(".cancel,.icon-remove-wrap").on("click",function(){
        $(".filter").css("display","none");
    });

    $(".filter>ul>li").on("click",function(){
        $(this).addClass("lichosed").siblings().removeClass("lichosed");
    });
    $(".confirm").on("click",function(){
        icon=$(".lichosed>span").html();
        $(".pic-show>input").val(icon);
        $(".filter").css("display","none");
        $(".pic-show>i").addClass(icon);
    });
    //传递数据
    $(".btn-last-confirm").on("click",function(){
        var parent=$(".select option:selected").val();  //父级
        var treeName=$(":input[name='tree_name']").val();
        var hiddenPic=$(".hidden-pic").val();

        if(treeName==""){
            layer.msg("请输入分类名称");
            flag=false;
        }else if(parent==""){
            layer.msg("请选择菜单父级");
            flag=false;
        }else if(hiddenPic==""){
            layer.msg("请选择图片");
            flag=false;
        }else{
            flag=true;
        }

        var datastr1=$(".form-horizontal").serialize();
        if(flag==true){
            ajax("post","/tree/add-tree",datastr1,function success(data){
                if(data.code==200){
                    layer.msg(data.msg);
                    setTimeout(function(){
                        location.href="/tree/index";
                    },1200)
                }else{
                    layer.msg(data.msg);
                }
            })
        }
    });
    $(".btn-last-cancel").on("click",function(){
        location.href='index';
    });
})
